//
//  CartOrderStatusController.m
//  JiaoX
//
//  Created by jiaoxiu－005 on 15/1/21.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "CartOrderStatusController.h"
#import "OrderStatusView.h"
#import "JXOrderViewController.h"

@interface CartOrderStatusController ()<OrderStatusViewDelegate>
{
    UIImageView *privateSendView;
}
@property(nonatomic,copy)NSString *resultCode;
@property(nonatomic,strong)OrderStatusView *statusView;
@property(nonatomic,assign)int type;
@end

@implementation CartOrderStatusController

+(instancetype)initWithResultCode:(NSString *)resultCode type:(int)type{
    CartOrderStatusController *result = [[CartOrderStatusController alloc]init];
    result.resultCode = resultCode;
    result.type = type;
    return result;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    UIBarButtonItem *item = [[UIBarButtonItem alloc]init];
    [item setTitle:@""];
    [self.navigationItem setLeftBarButtonItem:item];
    [self.navigationItem setTitle:@"订单状态"];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self setupStatusView];
    [self checkPayStatus];
}

/**
 *  检查支付状态
 */
-(void)checkPayStatus{
    if(![Utils isBlankString:_resultCode]){
    
        switch (_resultCode.integerValue) {
            case 9000:// 支付成功
                JXLog(@"支付成功");
                [self orderCommitSuccess];
                break;
            case 4000:// 系统异常
                JXLog(@"系统异常");
                [self orderCommitFailed:@"系统异常"];
                break;
            case 4001:// 订单参数错误
                JXLog(@"订单参数错误");
                [self orderCommitFailed:@"订单参数错误"];
                break;
            case 6001:// 用户取消支付
                JXLog(@"用户取消支付");
                [self orderCommitFailed:@"求主人带我回家吧"];
                break;
            case 6002:// 网络连接异常
                JXLog(@"网络连接异常");
                [self orderCommitFailed:@"网络连接异常"];
                break;
            default:
                break;
        }
    }
}

/**
 *  初始化状态页面
 */
- (void)setupStatusView {
    _statusView = [[OrderStatusView alloc]init];
    _statusView.x = (JXScreenW-_statusView.width)/2;
    _statusView.y = (JXScreenH-_statusView.height)/2;
    _statusView.delegate = self;
    [self.view addSubview:_statusView];
}

/**
 *  支付成功
 */
-(void)orderCommitSuccess{
    
    //用户现在是vip
    [UserDefaultsHelper setInteger:1 forKey:KEY_USER_LEVEL];
    
    [_statusView setStatusImg:@"order_status_success"];
    [_statusView setBtnText:@"查看订单"];
    [_statusView setOneText:@"订单提交成功"];
    [_statusView setTwoText:@"我们的快递员正在拼命的配送"];
    
    
    //保密配送动画
    privateSendView  = [[UIImageView alloc]init];
    UIImage *scaleImg  =  [UIImage imageNamed:@"private_distribution_icon_scale"];
    [privateSendView setImage:scaleImg];
    [privateSendView setFrame:CGRectMake(JXScreenW/3, JXScreenH/3-50, scaleImg.size.width + 200, scaleImg.size.height + 200)];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.8];
    [UIView setAnimationDelegate:self];
     privateSendView.transform = CGAffineTransformScale([self transformForOrientation], 0.3 , 0.3);
    [UIView setAnimationDidStopSelector:@selector(animFinish)];
    [UIView commitAnimations];
    [self.view addSubview:privateSendView];
}

-(void)animFinish{
    [privateSendView setImage:[UIImage imageNamed:@"private_distribution_icon"]];
}

- (CGAffineTransform)transformForOrientation {
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if (UIInterfaceOrientationLandscapeLeft == orientation) {
        return CGAffineTransformMakeRotation(M_PI*1.5);
    } else if (UIInterfaceOrientationLandscapeRight == orientation) {
        return CGAffineTransformMakeRotation(M_PI/2);
    } else if (UIInterfaceOrientationPortraitUpsideDown == orientation) {
        return CGAffineTransformMakeRotation(-M_PI);
    } else {
        return CGAffineTransformIdentity;
    }
}

/**
 *  支付失败
 *
 *  @param tips 提示语
 */
-(void)orderCommitFailed:(NSString *)tips{
    [_statusView setStatusImg:@"order_status_failure"];
    [_statusView setBtnText:@"重新支付"];
    [_statusView setOneText:@"订单支付失败"];
    [_statusView setTwoText:tips];
}

/**
 *  跳转到订单列表页面
 */
-(void)didNext{
    //关闭本页面
    [self.navigationController popViewControllerAnimated:NO];
    //判断回调谁
    switch (_type) {
        case ORDER_STATUS_CART:
            [_delegate orderStatusFinish:ORDER_STATUS_CART];
            break;
        case ORDER_STATUS_ORDERLIST:
            [_delegate orderStatusFinish:ORDER_STATUS_ORDERLIST];
            break;
        case ORDER_STATUS_ORDERDETAIL:
            [_delegate orderStatusFinish:ORDER_STATUS_ORDERDETAIL];
            break;
        default:
            break;
    }
}

@end
